﻿using System.NETool;
using System.Text;
using System.Xml;

namespace NETool.Demo;

internal class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
        Console.WriteLine("Hello world".StartsWithIgnoreCase("HA", "hello"));

        Console.WriteLine(Dir.VerifyFileName("AA"));
        Console.WriteLine(Dir.VerifyFileName("C:\\AA\\a"));
        Console.WriteLine(Dir.VerifyFileName("D:\\AB\\a"));

        byte[] rdm = RandomEx.Shared.NextBytes(2048);
        byte[] gzcBytes = rdm.GZipCompress();
        byte[] gzdBytes = gzcBytes.GZipDecompress();
        Console.WriteLine(rdm.AsSpan().SequenceEqual(gzdBytes));

        gzcBytes = rdm.ZipCompress();
        gzdBytes = gzcBytes.ZipDecompress();
        Console.WriteLine(rdm.AsSpan().SequenceEqual(gzdBytes));

        int taa = TestEnum.test1.Value();
        int tbb = TestEnum.test2.ByteValue();
        ushort tcc = TestEnum.test3.UShortValue();

        ConsoleEx.WriteLine(taa, true);
        ConsoleEx.WriteLine(tbb, ConsoleColor.Cyan);
        ConsoleEx.WriteLine(taa, tbb, tcc);
        ConsoleEx.WriteLine(taa);
        ConsoleEx.WriteLine(2, null);

        string file = @"D:\Bordar\Gateway\bdserver\Bin\net8.0\Config\ComConfig.xml";

        XmlConfig reader = new(file);
        reader.Load();
        string ServerIP = reader.Get("ServerIP");
        int ServerPort = reader.Get("ServerPort").ToInt();
        XmlNodeList nodes = reader.GetNodes("ComConfigs/ComConfig");
        foreach (XmlElement node in nodes)
        {
            Console.WriteLine(node.Get("Name"));
        }

        XmlConfig config = new(@"D:\ComConfig.xml");
        config.New("Root");
        config.Set("ServerIP", "AA");
        config.Upsert("ServerIP", "BB");
        config.Set("ServerPort", 123);
        config.Set("Temp", "/");
        var ComConfigs = config.NewNode("ComConfigs");
        for (int i = 0; i < 3; i++)
        {
            var ComConfig = ComConfigs.NewNode("ComConfig");
            ComConfig.Set("Name", "Name" + i);
            ComConfig.Set("Port", i.ToString());
        }

        var coms = config.NewNode("Coms");
        for (int i = 0; i < 3; i++)
        {
            coms.Set("Name", "Name" + i);
        }

        config.Save();

        config = new XmlConfig(@"D:\ComConfig.xml");
        config.Load();
        ServerIP = config.Get("ServerIP");
        ServerPort = config.Get("ServerPort").ToInt();
        string tmp = config.Get("Temp");
        nodes = config.GetNodes("ComConfigs/ComConfig");
        foreach (XmlNode node in nodes)
        {
            Console.WriteLine(node.Get("Name"));
        }

        nodes = config.GetNodes("Coms/Name");
        foreach (XmlElement node in nodes)
        {
            Console.WriteLine(node.InnerText);
        }

        double aa = 1.1;
        double bb = aa.Min(3);
        Console.WriteLine(bb);
        bb = aa.Min(0.5);
        Console.WriteLine(bb);

        int ab = 4;
        int ac = ab.Max(3);
        Console.WriteLine(ac);
        ac = ab.Max(5);
        Console.WriteLine(ac);

        Console.WriteLine(TestEnum.test1.Description());
        Console.WriteLine(TestEnum.test2.Description());
        Console.WriteLine(TestEnum.test3.Description());

        byte[] str = "Hello, World!".ToEnByteArray();
        string md5 = str.PBKDF2(1000, 16);
        bool b = str.PBKDF2Verify(md5, 1000);
        Console.WriteLine(md5);
        Console.WriteLine(b);

        string original = "Here is some data to encrypt!";

        // Encrypt the string to an array of bytes.
        var encrypted = original.AesEncrypt();

        // Decrypt the bytes to a string.
        string roundtrip = encrypted.AesDecrypt();

        //Display the original data and the decrypted data.
        Console.WriteLine("Original:   {0}", original);
        Console.WriteLine("Round Trip: {0}", roundtrip);

        byte[] buffer = Encoding.UTF8.GetBytes(original);
        byte[] deBytes = buffer.AesEncrypt("2", "3");
        byte[] enBytes = deBytes.AesDecrypt("2", "3");
        b = buffer.AsSpan().SequenceEqual(enBytes.AsSpan());
        Console.WriteLine(b);

        // Encrypt the string to an array of bytes.
        encrypted = original.DesEncrypt();

        // Decrypt the bytes to a string.
        roundtrip = encrypted.DesDecrypt();

        //Display the original data and the decrypted data.
        Console.WriteLine("Original:   {0}", original);
        Console.WriteLine("Round Trip: {0}", roundtrip);

        deBytes = buffer.DesEncrypt("2", "3");
        enBytes = deBytes.DesDecrypt("2", "3");
        b = buffer.AsSpan().SequenceEqual(enBytes.AsSpan());
        Console.WriteLine(b);

        original = original.Repeat(1000);
        RSAKey key = RSA.GenKey();
        byte[] s1 = RSA.EncryptByPrivateKey(original, key.PrivateKey);
        roundtrip = RSA.DecryptByPublicKey(s1, key.PublicKey);
        Console.WriteLine("Original:   {0}", original);
        Console.WriteLine("Round Trip: {0}", roundtrip.Equals(original));

        s1 = RSA.EncryptByPublicKey(original, key.PublicKey);
        roundtrip = RSA.DecryptByPrivateKey(s1, key.PrivateKey);
        Console.WriteLine("Original:   {0}", original);
        Console.WriteLine("Round Trip: {0}", roundtrip.Equals(original));
    }
}